package com.hlf.modules.api.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.api.service.QueueService;
import com.hlf.modules.merchant.table.entity.TableType;
import com.hlf.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 排队接口
 */
@RestController
@RequestMapping("/api/queue")
@Api("排队接口")
public class ApiQueueController {

    @Autowired
    private QueueService queueService;

    /**
     * 排队列表
     */
    @GetMapping("list")
    @ApiOperation(value = "排队列表", notes = "排队列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult list(@LoginUser SysUserEntity user) throws JsonProcessingException {
        List<TableType> tableTypes = queueService.queueList(user.getCurrentStoreId());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, tableTypes);
    }

    /**
     * 取号
     */
    @PostMapping("getNum")
    @ApiOperation(value = "取号", notes = "取号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableTypeId", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult getNum(String tableTypeId, @LoginUser SysUserEntity user) {
        Assert.isBlank(tableTypeId, "餐桌类型id不能为空!");

        Map<String, Object> map = queueService.getNum(user, tableTypeId);
        Date date = new Date();
        map.put("date",date);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

    /**
     * 叫号
     */
    @PostMapping("findNum")
    @ApiOperation(value = "叫号", notes = "叫号")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableTypeId", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "serialNumber", value = "排队序号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult findNum(@LoginUser SysUserEntity user, String tableTypeId, String serialNumber) {
        Assert.isBlank(tableTypeId, "餐桌类型id不能为空!");
        Assert.isBlank(serialNumber, "排队序号不能为空!");
        return queueService.findNum(user, tableTypeId, serialNumber);
    }

    /**
     * 下一位
     */
    @PostMapping("next")
    @ApiOperation(value = "下一位", notes = "下一位")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "tableTypeId", value = "餐桌类型id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "serialNumber", value = "排队序号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult next(@LoginUser SysUserEntity user, String tableTypeId, String serialNumber) {
        Assert.isBlank(tableTypeId, "餐桌类型id不能为空!");
        Assert.isBlank(serialNumber, "排队序号不能为空!");
        queueService.next(user, tableTypeId, serialNumber);
        return ReturnResult.ok();
    }

    /**
     * 打开/关闭每日清除排队号开关
     */
    @PostMapping("oorc")
    @ApiOperation(value = "打开/关闭每日清除排队号开关", notes = "打开/关闭每日清除排队号开关")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "dailyNum", value = "每日清除编号开关  1:打开  0:关闭", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult oorc(@LoginUser SysUserEntity user, String dailyNum) {
        Assert.isBlank(dailyNum, "每日清除编号不能为空!");
        return queueService.openOrClose(user,Byte.valueOf(dailyNum));
    }

    /**
     * 查询每日清除排队号开关状态
     */
    @GetMapping("status")
    @ApiOperation(value = "查询每日清除排队号开关", notes = "查询每日清除排队号开关")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult status(@LoginUser SysUserEntity user) {
        return queueService.queryStatus(user.getCurrentStoreId());
    }

}
